package views;
import DAO.MangerDao;
import Entity.Manger;
import util.NotNull;
import util.Result;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Objects;

import javax.swing.*;


public class JavaDemo6_1 {
    int sw = Toolkit.getDefaultToolkit().getScreenSize().width;
    int sh = Toolkit.getDefaultToolkit().getScreenSize().height;
    JTextField txtQ;
    JPasswordField txtW;
    JLabel txtq;
    JLabel txtw;
    JButton bt;
    JTextField txtE;

    class PanelBg extends JPanel {

        Dimension d;
        Image image;
        PanelBg(Dimension d, Image image){
            this.d = d;
            this.image = image;
        }
        @Override
        public  void  paintComponent(Graphics g ) {
            g.drawImage(image,0,0,d.width,d.height,this);
        }

    }
    Dimension fmsize = new Dimension(800,600);
    ImageIcon imageIcon = new ImageIcon(Objects.requireNonNull(this.getClass().getResource("/洱海2.jpg")));
    JavaDemo6_1() {

        int fmw = 500;
        int fmh = 300;

        JFrame fmlg = new JFrame();
        fmlg.setTitle("用 户 登 录");
        fmlg.setBounds((sw - fmw) / 2, (sh - fmh) / 2, fmw, fmh);
        fmlg.setDefaultCloseOperation(fmlg.EXIT_ON_CLOSE);

        PanelBg pContent = new PanelBg(fmsize, imageIcon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0, 0, fmw, fmh);
        pContent.setBackground(Color.pink);
        fmlg.setFocusable(true);

        txtQ = new JTextField();
        txtQ.setText("IC号 账号 等 ");
        txtQ.setBounds(100, 50, 200, 30);
        txtQ.setForeground(Color.lightGray);
        pContent.add(txtQ);
        txtQ.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                if (txtQ.getText().equals("IC号 账号 等 ")) {
                    txtQ.setCaretPosition(0);
                    txtQ.setText("");
                    txtQ.setForeground(Color.black);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (txtQ.getText().equals("")) {
//					txtQ.setCaretPosition(0);
                    txtQ.setText("IC号 账号 等 ");
                    txtQ.setForeground(Color.lightGray);
                }
            }
        });


        txtq = new JLabel("账 号");
        txtq.setBounds(60, 50, 200, 30);
        txtq.setForeground(Color.ORANGE);
        pContent.add(txtq);




        txtW = new JPasswordField();
        txtW.setText("密 码");
        txtW.setEchoChar('\0');
        txtW.setBounds(100, 90, 200, 30);
        txtW.setForeground(Color.lightGray);
        pContent.add(txtW);

        txtW.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                if (txtW.getText().equals("密 码")) {
                    txtW.setText("");
                    txtW.setEchoChar('*');

                    txtW.setForeground(Color.black);
                }
            }


            @Override
            public void focusLost(FocusEvent e) {
                if (txtW.getText().equals("")){

                    txtW.setText("密 码");
                    txtW.setEchoChar('\0');
                    txtW.setForeground(Color.lightGray);

                }


            }
        });


        txtw = new JLabel("密 码");
        txtw.setBounds(60, 90, 200, 30);
        txtw.setForeground(Color.orange);
        pContent.add(txtw);


        JButton bt  = new JButton("🤑");
        bt.setBounds(300, 90, 50, 30);
        bt.setBackground(Color.blue);
        bt.setForeground(Color.white);
        bt.setOpaque(false);
        pContent.add(bt);
        bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();

                if (cmd.equals("😄")){
                    txtW.setEchoChar('\0');
                    bt.setText("🙄");
                }
                else {
                    txtW.setEchoChar('*');
                    bt.setText("😄");
                }

            }
        });



        JButton btnlg = new JButton("登 录");
        btnlg.setBounds(100, 150, 70, 35);

        pContent.add(btnlg);
        btnlg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Manger manger=new Manger();
                MangerDao mangerDao=new MangerDao();
                manger.setAccount(txtQ.getText().trim());
                manger.setPassword(txtW.getText().trim());
                Manger shiji= mangerDao.Querry_login(manger.getAccount());
                if(shiji.getPassword().equals(manger.getPassword())){
                    Result result=new Result();
                    result.setStatus(shiji.isSenior()+"");
                    System.out.println(result.getStatus());
                    AdminiView adminiView=new AdminiView(result);
                    fmlg.dispose();
                }
                else {
                    JOptionPane.showMessageDialog(null,"您的密码输入有误");
                }
            }
        });


        JButton btnexit = new JButton("退 出");
        btnexit.setBounds(240, 150, 70, 35);
        pContent.add(btnexit);
        btnexit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fmlg.getContentPane().add(pContent);
        fmlg.setVisible(true);
    }

    public static void main (String[]args){
        new JavaDemo6_1();
    }

}





